<?php
namespace app\admin\model;
use think\Config;
use think\Db;
class BrandModel extends \app\common\model\CommonModel
{
    public function getList($params)
    {
        try {
            $page     = isset($params['page']) && is_numeric($params['page']) ? (int)$params['page'] : 1;
            $pagesize = isset($params['pagesize']) && is_numeric($params['pagesize']) ? (int)$params['pagesize'] : 10;
            $start = ($page-1) * $pagesize;
            $field = "a.id,a.brand,a.remark,a.create_userid,a.team_video,a.team_operate,a.create_time,b.username";
            $sql = "SELECT {$field} FROM brand a LEFT JOIN user b ON a.create_userid = b.userid WHERE 1=1 ";
            if(!empty($params['username'])){
                $sql .= "AND b.username LIKE '%{$params['username']}%' ";
            }
            if(!empty($params['brand'])){
                $sql .= "AND a.brand LIKE '%{$params['brand']}%' ";
            }
            $order = "ORDER BY create_time DESC LIMIT {$start},{$pagesize}";
            $csql = str_replace($field,'count(*)',$sql);
            $sql .= $order;
            $res = $this->getRes($sql);
            if ($res) {
                foreach ($res as $key => $value) {
                    $res[$key]['product'] = Db::table('product')->where(['brand_id' => $value['id']])->select();
                    $res[$key]['video'] = Db::table('video')->where(['brand_id' => $value['id']])->select();
                }
            }
            $count = $this->getCount($csql);
            return $this->pageData($res,$pagesize,$page,$count);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
}